<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 手机短信接口
 */
class Ruoyi extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        $this->model = new \app\admin\model\ProductsShopify;
        // $this->admin = Db::name('admin')->where('id', '=', $this->auth->id)->find();
    }

    public function listProducts()
    {
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:*');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');


        $where = array();
        $sort = 'updated_at';
        $order = 'desc';
        $offset = 0;
        $limit = 10;

        $where2 = array();
        $type = $this->request->param('type');
        
        $where3 = array();
        // if($this->admin['launch'] == 1){
        //     $where3['accounturl'] = ['in','55'];

        //     $wheregroup['createtime'] = array('elt',time());
        //     $wheregroup['updatetime'] = array('egt',time());
        //     $wheregroup['admin_id'] = $this->admin['id'];
        //     $accounturlgroup = Db::name('fedition_accounturlgroup')
        //         ->where($wheregroup)
        //         ->select();

        //     $accounturls = '';
        //     if(!empty($accounturlgroup)){
        //         foreach ($accounturlgroup as $k=>$v) {
        //             $accounturlgroup2[] = $v['accounturl'];
        //         }
        //         $accounturls = implode(',',$accounturlgroup2);
        //     }
        //     if(!empty($accounturls)){
        //         $where3['accounturl'] = ['in',$accounturls];
        //     }
        // }

        if($type == 1){
            $ytime = strtotime(date('Y-m-d')) - 1;
            $where2['updated_at']  = array('egt',$ytime);
        }

        $list = $this->model
            ->where($where)
            ->where($where2)
            ->where($where3)
            ->order($sort, $order)
            ->paginate($limit);
        $lists = $list->items();
            foreach ($lists as $k=>$v){
                $accounturl = Db::name('fedition_accounturl')->where('id', '=', $v['accounturl'])->find();
                if($accounturl){
                    $lists[$k]['accounturl_name'] = $accounturl['name'];
                }else{
                    $lists[$k]['accounturl_name'] = '店铺名称已删除';
                }
                $shopify_variants = Db::name('products_shopify_variants')->where('product_id',$v['id'])->select();
                $skus = '';
                if(!empty($shopify_variants)){
                    foreach ($shopify_variants as $key=>$val) {
                        if(!empty($val['sku'])){
                            if(!empty($skus)){
                                $skus = $skus.','.$val['sku'];
                            }else{
                                $skus = $val['sku'];
                            }
                        }
                    }
                }
                $lists[$k]['skus'] = $skus;
            }

        $Statistics = array();
        $number_goods = $this->model
            ->where($where)
            ->where($where2)
            ->where($where3)
            ->count();
        $been_updat = $this->model
            ->where($where)
            ->where($where2)
            ->where($where3)
            ->where('updatestatus','normal')
            ->count();
        $not_updat = $this->model
            ->where($where)
            ->where($where2)
            ->where($where3)
            ->where('updatestatus','hidden')
            ->count();
        $update_error = $this->model
            ->where($where)
            ->where($where2)
            ->where($where3)
            ->where('updatestatus','unavailable')
            ->count();
        $Statistics['number_goods'] =  $number_goods;
        $Statistics['number_goods2'] =  $number_goods;
        $Statistics['been_updat'] =  $been_updat;
        $Statistics['been_updat2'] =  $been_updat;
        $Statistics['not_updat'] =  $not_updat;
        $Statistics['not_updat2'] =  $not_updat;
        $Statistics['update_error'] =  $update_error;
        $Statistics['update_error2'] =  $update_error;

        $result = array("code" => 200,"msg" => "查询成功","total" => $list->total(), "rows" => $lists, "Statistics" => $Statistics);
        return json($result);
    }
    
}
